package myProject.gwt.client.simpleTable;

import myProject.gwt.client.dynamicTable.ScrollBar;

import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class AbstractTable extends Composite {

	private VerticalPanel verticalPanel = new VerticalPanel();
	private HorizontalPanel horizontalPanel = new HorizontalPanel();
	private ScrollPanel sp = new ScrollPanel();
	private SimplePanel sp2 = new SimplePanel();
	private HorizontalPanel headerPanel = new HorizontalPanel();
	private SimplePanel dummyHeaderCell = new SimplePanel();
	private ScrollBar horizontalSB = new ScrollBar(true); 	
	private ScrollBar verticalSB = new ScrollBar(false);
	private boolean fakeScrollBar;
	
	public AbstractTable() {
		super();
		initWidget(horizontalPanel);
		horizontalPanel.add(verticalPanel);
		sp.setStylePrimaryName("scrollPanel");
		sp2.setStylePrimaryName("scrollPanel2");
		setStylePrimaryName("table");
		horizontalSB.setWidth("800px");
		verticalSB.setHeight("600px");
				
		
		ScrollHandler sh = new ScrollHandler(){
			@Override
			public void onScroll(ScrollEvent event) {
				onBeforeScroll();
				onAfterScroll();
			}			
		};
		
		horizontalSB.addScrollHandler(sh);
		verticalSB.addScrollHandler(sh);
		
		horizontalPanel.add(verticalSB);
	}
	
	
	protected void init(){
		verticalPanel.add(headerPanel);
		verticalPanel.add(sp);
		verticalPanel.add(horizontalSB);
		sp2.add(getHeader().getComponent());
		headerPanel.add(sp2);
		headerPanel.add(dummyHeaderCell);
		
		sp.add(getContent());
		sp.setAlwaysShowScrollBars(true);
		sp.setScrollPosition(0);
		sp.setHorizontalScrollPosition(0);
				
		sp.addScrollHandler(new ScrollHandler(){

			@Override
			public void onScroll(ScrollEvent event) {
				int sl = sp.getHorizontalScrollPosition();
				
				//sl = Math.min(sl, sp.getWidget().getOffsetWidth()-sp.getOffsetWidth()+25);
				DOM.setElementPropertyInt(sp2.getElement(), "scrollLeft", sl);
				AbstractTable.this.onScroll();
			}
			
		});
		
		dummyHeaderCell.setPixelSize(16, 20);
	}
	
	protected int getColumnWidth(int columnIndex){
		return getHeader().getHeaderCell(columnIndex).getWidth()+1;
	}
	
	protected void onAfterScroll() {
		sp.setScrollPosition(verticalSB.getScrollPosition());		
		sp.setHorizontalScrollPosition(horizontalSB.getScrollPosition());
	}
	
	protected void onBeforeScroll(){
		
	}
	
	protected void onScroll(){
		
	}
	
	protected int getScrollPostion(){
		return fakeScrollBar ? verticalSB.getScrollPosition() : sp.getScrollPosition();
	}
	
	protected int getHorizontalScrollPostion(){
		return fakeScrollBar ? horizontalSB.getScrollPosition() : sp.getHorizontalScrollPosition();
	}
	
	protected abstract IHeader getHeader();
	
	protected abstract Widget getContent();
	
	public void setFakeScrollBar(boolean fakeScrollBar) {
		this.fakeScrollBar = fakeScrollBar;
	}


	public boolean isFakeScrollBar() {
		return fakeScrollBar;
	}
	
	

}
